local extension = Package:new("beizhong")
extension.extensionName = "Euphonium"
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
    ["beizhong"] = "北中",
}

--高太尉
local gaobanlinai = General:new(extension,"gaobanlinai","th_k_bei",4,4,General.Female)
local lingdui = fk.CreateTriggerSkill{
    name = "lingdui",
    anim_type = "offensive",
    frequency = Skill.Compulsory,
    events = {fk.EventPhaseChanging, fk.EventPhaseStart},
    can_trigger = function(self, event, target, player, data)
      if target == player and player:hasSkill(self) then
        if event == fk.EventPhaseChanging then
          return data.to == Player.Start
        else
          return player.phase == Player.Play and player:getMark("lingdui-phase") > 0
        end
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      if event == fk.EventPhaseChanging then
        room:setPlayerMark(player, "lingdui-phase", 1)
        player:gainAnExtraPhase(Player.Play)
      else
        room:setPlayerMark(player, "lingdui-phase", 0)
        if room:askForSkillInvoke(player, self.name, nil, "#lingdio-invoke") then
          room:loseHp(player, 1, self.name)
          if not player.dead then
            local cards = room:getCardsFromPileByRule("slash", 1, "discardPile")
            if #cards > 0 then
              room:obtainCard(player, cards[1], true, fk.ReasonJustMove)
            end
          end
        end
      end
    end,
  }
gaobanlinai:addSkill(lingdui)
  local lengli = fk.CreateActiveSkill{
      name = "lengli",
      anim_type = "control",
      prompt = "#lengli-active",
      can_use = function(self, player)
        return player:usedSkillTimes(self.name) == 0
      end,
      card_num = 0,
      target_num = 1,
      target_filter = function(self, to_select, selected)
        local player = Fk:currentRoom():getPlayerById(to_select)
        return #selected == 0 and player:getEquipment(Card.SubtypeArmor)
      end,
        card_filter = Util.FalseFunc,
        on_use = function(self, room, effect)
          local player = room:getPlayerById(effect.from)
          local target = room:getPlayerById(effect.tos[1])
          local id = target:getEquipment(Card.SubtypeArmor)
          room:throwCard({id}, self.name, target, player)
          if id>0 then
            target:drawCards(1, self.name)
          end
          end
  }
gaobanlinai:addSkill(lengli)
local honggou = fk.CreateViewAsSkill{
  name = "honggou",
  anim_type = "offensive",
  pattern = "slash",
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("slash")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    use.extra_data = use.extra_data or {}
    use.extra_data.honggou = player.id
  end,
  after_use = function(self, player, _)
    player:drawCards(2)
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  enabled_at_response = Util.FalseFunc,
  }
  local honggouBuff = fk.CreateTargetModSkill{
    name = "#honggou_buff",
    bypass_distances =  function(self, player, skill, card, to)
      return table.contains(card.skillNames, honggou.name)
    end,
  }
  gaobanlinai:addSkill(honggou)
  honggou:addRelatedSkill(honggouBuff)
Fk:loadTranslationTable{
    ["gaobanlinai"] = "高坂丽奈",
    ["#gaobanlinai"] = "特立独行",
    ["lingdui"] = "领队",
    [":lingdui"] = "锁定技，回合开始时，你执行一个额外的出牌阶段，此阶段开始时你可以选择是否失去1点体力并从弃牌堆获得一张【杀】。",
    ["#lingdio-invoke"] = "领队：是否失去一点体力获得一张杀",
    ["lengli"] = "冷厉",
    [":lengli"] = "出牌阶段限一次，你可以弃置一名角色装备区里一张防具牌，然后令其摸一张牌。",
    ["#lengli-active"] = "选择一名角色，弃置其防具牌并令其摸一张牌",
    ["honggou"] = "鸿沟",
    [":honggou"] = "每回合限一次，你可以将一张牌当做一张无距离限制的杀使用，然后你摸两张牌。",
    ["#honggou"] = "选择一张牌当【杀】对 %dest 使用",
}
local huangqianjiumeizi = General:new(extension,"huangqianjiumeizi","th_k_bei",4,4,General.Female)
local weiren = fk.CreateTriggerSkill{
  name = "weiren",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,
}
local weiren_trigger = fk.CreateTriggerSkill{
  name = "#weiren_trigger",
  mute = true,
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if player.phase == Player.Finish and player:usedSkillTimes("weiren", Player.HistoryTurn) > 0 and not player.dead then
      local n = 0
          for _, e in ipairs(player.room.logic:getActualDamageEvents(999, function(e) return e.data[1].from == player end)) do
            local damage = e.data[1]
            n = n + damage.damage
          end
          self.cost_data = n
          return n == 0
        end
      end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = self.cost_data
    if n == 0 then
    player:broadcastSkillInvoke("weiren")
    room:notifySkillInvoked(player, "weiren", "negative")
    room:loseHp(player, 1, "weiren")
    end
  end,
}
huangqianjiumeizi:addSkill(weiren)
weiren:addRelatedSkill(weiren_trigger)
local weiren_ex = fk.CreateTriggerSkill{
  name = "weiren_ex",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,
}
local weiren_ex_trigger = fk.CreateTriggerSkill{
  name = "#weiren_ex_trigger",
  mute = true,
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if player.phase == Player.Finish and player:usedSkillTimes("weiren_ex", Player.HistoryTurn) > 0 and not player.dead then
      local n = 0
          for _, e in ipairs(player.room.logic:getActualDamageEvents(999, function(e) return e.data[1].from == player end)) do
            local damage = e.data[1]
            n = n + damage.damage
          end
          self.cost_data = n
          return n == 0
        end
      end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = self.cost_data
    if n == 0 then
    player:broadcastSkillInvoke("weiren_ex")
    room:notifySkillInvoked(player, "weiren_ex", "negative")
    room:loseHp(player, 1, "weiren_ex")
    end
  end,
}
weiren_ex:addRelatedSkill(weiren_ex_trigger)
local eup_juesheng = fk.CreateTriggerSkill{
  name = "eup_juesheng",
  anim_type = "defensive",
  events = {fk.EnterDying},
  frequency = Skill.Limited,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper),
      1, 1, "#eup_juesheng-choose", self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:throwAllCards("jhe")
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
    local to = room:getPlayerById(self.cost_data.tos[1])
    local use = room:useVirtualCard("duel", nil, player, to, self.name, false)
        if use.damageDealt then
    room:changeMaxHp(player, 1)
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
    player:drawCards(3,self.name)
    room:handleAddLoseSkills(player, "weiren_ex", nil, true, false)
    room:handleAddLoseSkills(player, "-weiren", nil, true, false)
    room:handleAddLoseSkills(player, "eup_zhizhen", nil, true, false)
  end
end,
}
huangqianjiumeizi:addSkill(eup_juesheng)
huangqianjiumeizi:addRelatedSkill(weiren_ex)
local eup_zhizhen = fk.CreateTriggerSkill{
  name = "eup_zhizhen",
  anim_type = "offensive",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase ~= Player.Draw and not player:isKongcheng() then
      local currentplayer = player.room.current
      if currentplayer and currentplayer.phase <= Player.Finish and currentplayer.phase >= Player.Start then
        local x = 0
        for _, move in ipairs(data) do
          if move.to == player.id and move.toArea == Card.PlayerHand then
            x = x + #move.moveInfo
          end
        end
        return x > 1
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(table.filter(room.alive_players, function(p)
      return player:canPindian(p) end), Util.IdMapper),
      1, 1, "#eup_zhizhen-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local pindian = player:pindian({to}, self.name)
      if pindian.results[to.id].winner == player then
        local use = room:useVirtualCard("slash", nil, player, to, self.name, false)
        if use.damageDealt then 
          local n = player:getHandcardNum() - player.hp
              if n < 0 then
                player:drawCards(-n, self.name)
              else
                player.room:recover{
                  who = player,
                  num = math.min(n, player:getLostHp()),
                  recoverBy = player,
                  skillName = self.name
                  }
              end
        end
      end
  end,
}
huangqianjiumeizi:addRelatedSkill(eup_zhizhen)
Fk:loadTranslationTable{
  ["huangqianjiumeizi"] = "黄前久美子",
  ["#huangqianjiumeizi"] = "决意的终章",
  ["weiren"] = "委任",
  [":weiren"] = "锁定技：准备阶段，你可以摸两张牌,若如此做,结束阶段,若本回合你未造成过伤害，你失去1点体力。",
  ["eup_juesheng"] = "绝生",
  [":eup_juesheng"] = "限定技:当你进入濒死时,你可以弃置区域内所有牌视为使用一张酒，然后视为使用一张“独奏之争”，若此牌造成伤害，你回复1点体力,摸三张牌并增加1点体力上限，修改“委任”为非锁定技，然后获得“臻善”。",
  ["#eup_juesheng-choose"] = "绝生：选择一名角色视为对其使用一张决斗。",
  ["weiren_ex"] = "委任",
  [":weiren_ex"] = "准备阶段，你可以摸两张牌,若如此做,结束阶段,若本回合你未造成过伤害，你失去1点体力。",
  ["eup_zhizhen"] = "臻善",
  [":eup_zhizhen"] = "当你不因摸牌阶段而获得至少两张牌时,你可以与一名角色进行拼点;若你赢，则你视为对其使用一张没有次数限制的杀;若此杀造成伤害,你可以将手牌摸至与体力值相同或将体力回复至与手牌数相同",
  ["#eup_zhizhen-choose"] = "臻善:选择一名角色，与其拼点"
}

return extension